<?php
namespace app\common\library;

class common
{
    /**
     *  http or https
     */
    public static function httptype()
    {
        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';

        return $http_type;
    }

    /**
     *求两个已知经纬度之间的距离,单位为千米
     * @param lng1,lng2 经度
     * @param lat1,lat2 纬度
     * @return float 距离，单位千米
     **/
    public static function distance($lng1, $lat1, $lng2, $lat2)//根据经纬度计算距离
    {
        //将角度转为弧度
        $radLat1 = deg2rad($lat1);
        $radLat2 = deg2rad($lat2);
        $radLng1 = deg2rad($lng1);
        $radLng2 = deg2rad($lng2);
        $a = $radLat1 - $radLat2;//两纬度之差,纬度<90
        $b = $radLng1 - $radLng2;//两经度之差纬度<180
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137;
        return $s;
    }

    /**
     * @content 生成token
     */
    public static function createToken()
    {
        $randChar = static::getRandChar(32);
        $timestamp = $_SERVER['REQUEST_TIME_FLOAT'];
        $tokenSalt = '63KAGxtT7bjATzwP';
        return md5($randChar . $timestamp . $tokenSalt);
    }

    /**
     * @content 返回任意字符串
     */
    public static function getRandChar($length)
    {
        $str = null;
        $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        $max = strlen($strPol) - 1;

        for ($i = 0;
             $i < $length;
             $i++) {
            $str .= $strPol[rand(0, $max)];
        }

        return $str;
    }


    
    /**
     * 获取随机字符串
     *
     * @param $length
     * @param bool $numeric
     * @return string
     * @author lxh
     */
    public static function random($length, $numeric = false)
    {
        $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
        $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));

        $hash = '';
        if (!$numeric) {
            $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
            $length--;
        }

        $max = strlen($seed) - 1;
        for ($i = 0; $i < $length; $i++) {
            $hash .= $seed[mt_rand(0, $max)];
        }

        return $hash;
    }
    
    //get请求
    public static function getHttp($url){
        $ch=curl_init();
        //设置传输地址
        curl_setopt($ch, CURLOPT_URL, $url);
        //设置以文件流形式输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_ENCODING,'');
        //接收返回数据
        $data=curl_exec($ch);
        curl_close($ch);
        $jsonInfo=json_decode($data,true);

        return $jsonInfo;
    }


    /**
     *  请求点评管家 加密
     */
    public static function getSign($params = array())
    {
        if(!empty($params)){
            $p =  ksort($params);
            if($p){
                $str = '';
                foreach ($params as $k=>$val){
                    $str .= $k . $val;
                }
                $strs = rtrim($str, '&');
                return $strs;
            }
        }
        return '参数错误';
    }

    /**
     *   请求点评管家
     */
    public static function curl_post($url,$data,$header){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);

        $result = curl_exec($ch);

        curl_close($ch);
        return $result;
    }
    
    /** 
     * 
     *  post 请求
     */
     
    public static function curl_post2($url,$data,$headers)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);

  
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    /**
     *   请求点评管家 get
     */
    public static function curl_get($url,$header){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);//设置链接
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//设置SSL验证
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
        $data = curl_exec($ch);//接收返回信息
        curl_close($ch);
        return $data;
    }

    /**
     *  生成订单号
     */
    public static function orderOn()
    {
        $order_id_main = date('YmdHis') . rand(10000000, 99999999);
        $order_id_len = strlen($order_id_main);
        $order_id_sum = 0;
        for ($i = 0; $i < $order_id_len; $i++) {
            $order_id_sum += (int)(substr($order_id_main, $i, 1));
        }
        $osn = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100, 2, '0', STR_PAD_LEFT);

        return $osn;
    }
    
    public static function curlGet($url,$header)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
    
    
    /**
     *  合成图片
     */
    public static function createImg($bgimg,$qrcode,$path_img)
    {
        $bigImg = imagecreatefromstring(file_get_contents($bgimg));
        $qCodeImg = imagecreatefromstring(file_get_contents($qrcode));
        
        $dstx = 380;
        $dsty = 900;
        
        list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qrcode);

        imagecopymerge($bigImg, $qCodeImg, $dstx, $dsty, 0, 0, $qCodeWidth, $qCodeHight, 100);
        
        imagejpeg($bigImg, $path_img);
        
        imagedestroy($bigImg);
        imagedestroy($qCodeImg);
    }
    
}


